<template>
	<view class="container">
		<view class="status_bar">
			<view style="margin-left: 3%;" @tap="goBack">
				<u-icon name="arrow-leftward" size='24' />
			</view>
			<view v-if="originalArray && Object.keys(originalArray).length > 0"
				style="margin-left: -50upx; font-weight: bold;">
				添加群
			</view>
			<view v-else style="margin-left: -50upx; font-weight: bold;">添加好友</view>
			<view></view>
		</view>

		<view class="shop-box">
			<view class="search-box" style="display: flex; align-items: center;"
				v-if="!originalArray || Object.keys(originalArray).length === 0">
				<u-input placeholder="请输入用户id" border="surround" v-model="user_uid"></u-input>
				<uni-icons @click="shanchu" type="closeempty" style="width: 30px;" size="18"
					color="#515151"></uni-icons>
				<uni-icons @click="getuserInfo" type="search" style="width: 30px;" size="18"
					color="#515151"></uni-icons>
			</view>

			<view class="box"  v-if="JSON.stringify(userInfo) != '{}'">
				<view class="box1" v-if="usercode === 1">
					<image
						:src="`http://chat.zwsmcn.com:8090/UserAvatarDownloader?action=ad&user_uid=${userInfo.user_uid}&user_local_cached_avatar=${userInfo.user_avatar_file_name}&enforceDawnload=1&token=`"
						class="imgs"></image>
					<view class="box2">
						<view>昵称：{{ userInfo.nickname }}</view>
						<view>ID：{{ userInfo.user_uid }}</view>
						<view>性别：{{ userInfo.user_sex === 1 ? '男' : '女' }}</view>
					</view>
				</view>
				<view v-if="usercode === 1">个性签名：{{ userInfo.user_desc }}</view>
			</view>
			<view class="" v-if="JSON.stringify(userInfo) != '{}'">
				<u-button v-if="usercode === 2" style="margin-top:10px" text="添加群" @click="addcentent"></u-button>
				<u-button v-else-if="usercode !== 2 && this.userInfo.user_uid == myuserId" style="margin-top:10px" text="不能添加自己"
					></u-button>
				<u-button v-else-if="usercode !== 2  && !is_friend && this.user_uid != myuserId" style="margin-top:10px" text="添加"
					@click="getcentent"></u-button>
				
				<u-button v-else style="margin-top:10px" text="已经是好友"></u-button>
				
				
				<u-button v-if="usercode !== 2  && !is_friend  && this.user_uid != myuserId" style="margin-top:10px" text="临时聊天" @click="toDetail()"></u-button>
			</view>
			
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				myuserId:uni.getStorageSync('userId'),
				usercode: 0,
				is_friend: false,
				userInfo: {},
				user_uid: '',
				g_uid: '',
				originalArray: {}
			};
		},
		onLoad(props) {
			if (props.user_uid) {
				this.user_uid = props.user_uid;
			}
			if (props.g_uid) {
				this.g_uid = props.g_uid;
			}

			if (!this.g_uid) {
				this.getuserInfo();
			} else {
				this.getfriendlist();
			}
		},
		methods: {
			shanchu() {
				this.user_uid = "";
				this.userInfo = {};
				this.getuserInfo();
			},
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			toDetail() {
				let typeu = 42;
				
				this.$api.globalData = this.userInfo;
				uni.setStorageSync('globalData', this.$api.globalData)
				this.toPage('/pages/index/index?typeu=' + typeu);
				// this.toPage('/pages/index/index?typeu=' + typeu + '&groupId=' + JSON.stringify(item));
			},
			getfriendlist() {
				const requestData = {
					processorId: '1016',
					jobDispatchId: '25',
					actionId: '8',
					newData: JSON.stringify({
						gid: this.g_uid
					})
				};

				uni.request({
					url: 'http://chat.zwsmcn.com:8090/rest_post',
					header: {
						'content-type': 'application/json'
					},
					data: JSON.stringify(requestData),
					method: 'POST',
					success: res => {
						this.usercode = 2;
						this.originalArray = JSON.parse(res.data.returnValue);
					}
				});
			},
			getuserInfo() {
				uni.request({
					url: this.$api.baseUrl + '/api/user/detail',
					header: {
						'content-type': 'application/json'
					},
					data: {
						user_uid: this.user_uid,
						my_id: uni.getStorageSync('userId')
					},
					method: 'POST',
					success: res => {
						console.log(res);
						this.userInfo = res.data.data || {};
						this.is_friend = res.data.data?.is_friend || false;
						this.usercode = res.data.data?.user_uid ? 1 : 0;
					}
				});
			},
			addcentent() {
				const requestData = {
					processorId: '1016',
					jobDispatchId: '24',
					actionId: '24',
					newData: JSON.stringify({
						invite_to_gid: this.g_uid
					})
				};

				uni.request({
					url: 'http://chat.zwsmcn.com:8090/rest_post',
					header: {
						'content-type': 'application/json'
					},
					data: JSON.stringify(requestData),
					method: 'POST',
					success: () => this.$util.toast('添加成功')
				});
			},
			getcentent() {
				const requestData = {
					processorId: '1008',
					jobDispatchId: '3',
					actionId: '24',
					newData: JSON.stringify({
						friendUserUid: this.userInfo.user_uid,
						localUserUid: uni.getStorageSync('user_uid'),
						desc: '1'
					})
				};

				uni.request({
					url: 'http://chat.zwsmcn.com:8090/rest_post',
					header: {
						'content-type': 'application/json'
					},
					data: JSON.stringify(requestData),
					method: 'POST',
					success: () => this.$util.toast('添加成功')
				});
			}
		}
	};
</script>
<style lang="scss" scoped>
	.box {
		width: 100%;
		margin-top: 10px;
		border: 1px solid #eeeeee;
		padding: 10px;
		// height: 200px;

		.box1 {
			width: 100%;
			// height: 100px;
			display: flex;

			.imgs {
				width: 80px;
				height: 80px;
				border-radius: 6px;
			}

			.box2 {
				flex: 1;

				view {
					font-weight: 600;
					margin-top: 6px;
				}
			}
		}
	}

	.container {
		width: 100%;
		height: 100%;
	}

	.status_bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 50upx;
	}

	.status_bar,
	.head {
		background-color: #fff;
		height: 160upx;

		.face {
			width: 160upx;
			height: 160upx;
			border-radius: 10upx;
		}

		.info-name {
			font-size: 40upx;
			font-weight: bold;
		}

		.info-num {
			// width: 56upx;
			padding: 2upx 30upx;
			background: #f0f4f7;
			border-radius: 30upx;
			margin-top: 21upx;
		}

		.copybtns {
			color: #4457a4;
			margin-top: 21upx;
		}
	}


	.shop-box {
		width: 92%;
		height: 36%;
		margin-left: 4%;
		// background-color: #f8f9fb;
		margin-top: 20upx;
		border-radius: 20upx;
		// padding: 10upx 30upx;
		color: #2e2e2e;
		font-weight: 300;

		image {
			width: 70upx;
			height: 70upx;
			padding: 10upx 28upx 0upx 10upx;

		}

		.con-shop {
			height: 100upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 30upx;
		}
	}
</style>